package com.chixing.controller;


import com.chixing.entity.Comment;
import com.chixing.service.CommentService;
import com.chixing.util.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwenjing
 * @since 2025-10-07
 */
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    private final CommentService service;


    @Autowired
    public CommentController(CommentService service) {
        this.service = service;
    }


    // 发布顶层评论
    @PostMapping("/save")
    public ResponseEntity<Comment> post(@RequestBody Comment comment) {
        comment.setParentId(null); // 确保为顶层
        Comment saved = service.postComment(comment);
        return ResponseEntity.ok(saved);
    }


    // 回复某条评论
    @PostMapping("/reply/{parentId}")
    public ResponseEntity<Comment> reply(@PathVariable String parentId, @RequestBody Comment reply) {
        Comment saved = service.reply(parentId, reply);
        return ResponseEntity.ok(saved);
    }


    // 点赞
    @PostMapping("/like/{id}")
    public ResponseEntity<Void> like(@PathVariable String id) {
        service.like(id);
        return ResponseEntity.ok().build();
    }


    // 获取电影的评论树（根 -> 子 -> 孙）
    @GetMapping("/movie/{movieId}")
    public ResponseEntity<List<Comment>> getByMovie(@PathVariable String movieId) {
        List<Comment> tree = service.getCommentsTreeByMovieId(movieId);
        return ResponseEntity.ok(tree);
    }

    //根据用户id查看我的评论


    /**
     * 根据用户id查看我的评论（时间倒序）
     */
    @GetMapping("/my")
    public Result getMyComments(@RequestParam("userId") String userId) {
        List<Comment> list = service.getCommentsByUserId(userId);
        return Result.getSuccess(list);
    }
}

